const fs = require("fs");
const path = require("path");

const gitHookPath = path.resolve("../../.git/hooks");

new Promise((resolve) => {
  fs.mkdir(gitHookPath, { recursive: true }, (err) => {
    if (err) {
      throw err;
    }
    resolve();
  });
}).then(() => {
  const shell = `#!/usr/bin/env bash\n` +
    `export PATH=/usr/local/bin:$PATH\n` +
    `node "${path.resolve(__dirname, "pre-commit.js")}"`;
  const filePath = path.resolve(gitHookPath, "pre-commit");
  fs.writeFile(filePath, shell, (error) => {
    if (error) {
      console.log(error);
    }
  });
  fs.chmodSync(filePath, '755');
});
